package atm;

import lovo01.ATM04.FileUtil;
import lovo01.ATM04.Users;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 取款界面
 */
public class GetPanel extends JPanel implements ActionListener {
    private ATM05 atm05;
    private JLabel getLabel = new JLabel("请输入取款金额:");
    private JTextField getTextField = new JTextField();
    private JButton sureBtn = new JButton(new ImageIcon("images/buttonSure.jpg"));
    private JButton offBtn = new JButton(new ImageIcon("images/buttonOff.jpg"));
    private Font font = new Font("宋体",Font.BOLD,25);
    public GetPanel(ATM05 atm05){
        this.atm05 = atm05;
        this.setLayout(null);
        this.setOpaque(false);

        getLabel.setBounds(300,200,200,50);
        getLabel.setFont(font);
        getTextField.setBounds(300,260,300,50);
        getTextField.setFont(font);
        sureBtn.setBounds(320,340,100,50);
        offBtn.setBounds(450,340,100,50);
        sureBtn.addActionListener(this);
        offBtn.addActionListener(this);

        this.add(getLabel);
        this.add(getTextField);
        this.add(sureBtn);
        this.add(offBtn);

    }

    public JTextField getGetTextField() {
        return getTextField;
    }

    public void setGetTextField(JTextField getTextField) {
        this.getTextField = getTextField;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == sureBtn) {
            Users loginUser = atm05.getLoginUser();
            int money = 0;
            try {
                String money02 = getTextField.getText();
                money = Integer.parseInt(money02);
            }catch (NumberFormatException e1){
                JOptionPane.showMessageDialog(this,"输入错误,请重新输入");
                getTextField.setText(null);
                return;
            }
            if (loginUser.getMoney() < money) {
                JOptionPane.showMessageDialog(this, "余额不足");
                return;
            } else {
                loginUser.setMoney(loginUser.getMoney() - money);
//                atm05.getUsers().get()
                FileUtil.writeObject(atm05.getUsers(),ATM05.FILE_PATH);
                JOptionPane.showMessageDialog(this, "取款成功");
            }
            getTextField.setText("");
            atm05.showPanel("menu");
        }
        if (e.getSource().equals(offBtn)) {
            getTextField.setText("");
            atm05.showPanel("menu");
        }
    }
}
